package cn.jbolt.core.model;

import cn.jbolt.core.annotation.JBoltAutoCache;
import cn.jbolt.core.annotation.TableBind;
import cn.jbolt.core.annotation.UnProcessBoolean;
import cn.jbolt.core.base.JBoltIDGenMode;
import cn.jbolt.core.bean.Option;
import cn.jbolt.core.bean.OptionBean;
import cn.jbolt.core.cache.JBoltDictionaryCache;
import cn.jbolt.core.cache.JBoltDictionaryTypeCache;
import cn.jbolt.core.cache.JBoltGlobalConfigCache;
import cn.jbolt.core.model.base.BaseDictionary;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
@UnProcessBoolean("sn")
@JBoltAutoCache(keyCache = true,column = "sn",bindColumn = "type_key")
@TableBind(dataSource = "main" , table = "jb_dictionary" , primaryKey = "id" , idGenMode = JBoltIDGenMode.SNOWFLAKE)
public class Dictionary extends BaseDictionary<Dictionary> {
	@Override
	protected void deleteExtraCache(String action) {
		JBoltDictionaryCache.me.removeEnableDatasCacheByTypeId(getTypeId());
	}

	public void cancelSystemDictionaryDeleteEnable(){
		put("cancelSystemDictionaryDeleteEnable",true);
	}
	public boolean _isCancelSystemDictionaryDeleteEnable(){
		Boolean value = getBoolean("cancelSystemDictionaryDeleteEnable");
		return value == null? false :value;
	}
	@Override
	protected String checkCanDelete() {
		if(!_isCancelSystemDictionaryDeleteEnable()){
			if(!JBoltGlobalConfigCache.me.getSystemDictionaryDeleteEnable()){
				return "系统安全设置做了限制，不能删除字典数据";
			}
		}
		Boolean isBuildIn = getIsBuildIn();
		if(isBuildIn!=null && isBuildIn){
			return "字典数据为系统内置类型，不能删除";
		}
		return null;
	}

	/**
	 * 系统内置类型里的自定义item
	 * @return
	 */
	public boolean getInnerCustom(){
		Boolean isBuildIn = getIsBuildIn();
		if(isBuildIn == null || isBuildIn){
			return false;
		}
		return JBoltDictionaryTypeCache.me.isBuildIn(Long.valueOf(getTypeId()));
	}

	public Option toOption(){
		return new OptionBean(getName(),getSn());
	}
}
